package com.qinge.blog.utils.mybatis.interceptor;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author QDW
 * @date 2022/5/7 20:03
 * @description MyBatis Field帮助类
 **/
public class FieldUtil {
    /**
     * @author QDW
     * @date 2022/5/7 19:03
     * @description 如果是对象获取所有属性
     * @param object
     * @return java.lang.reflect.Field[]
     **/
    public static Field[] getAllFields(Object object) {
        List<Field> fieldList = new ArrayList<>();
        Class<?> clazz = object.getClass();

        while (clazz != null) {
            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }

        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);

        return fields;
    }

    /**
     * @author QDW
     * @date 2022/5/7 19:01
     * @description 如果是列表 直接获取属性
     * @param object
     * @return java.util.List<java.lang.reflect.Field[]>
     **/
    public static List<Field[]> getListAllFields(Object object) {
        List<Field[]> fieldList = new ArrayList();

        if(object instanceof List){
            Field[] fields = null;
            for(Object o : (List)object){
                fields =getAllFields(o);
                fieldList.add(fields);
            }
        }

        return fieldList;
    }

}
